package wnhz.com.business.groupshop.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.R;
import wnhz.com.business.groupshop.adapter.CollectAdapter;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.BaseRecyclerAdapter;
import wnhz.com.business.groupshop.bean.CollectBean;
import wnhz.com.business.uitls.decoration.HorizDecoration;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.ToastUtils;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBar;

public class MyCollectActivity extends BaseActivity implements ActionBarClickListener, BaseRecyclerAdapter.OnDataLoadFinish {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.empty_layout)
    LinearLayout layEmtpy;
    @BindView(R.id.tv_msg)
    TextView tv_msg;
    @BindView(R.id.tv_collect)
    TextView tv_collect;

    private CollectAdapter collectAdapter;
    private List<CollectBean.InfoBean.GoodinfoBean> collectInfoList = new ArrayList<>();

    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, MyCollectActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collect);
        ButterKnife.bind(this);
        actionbar.setData("我的收藏", R.drawable.ic_lefft_white, null, 0, null,0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
//        initView();
    }

    private void initView() {
        collectAdapter = new CollectAdapter(this, collectInfoList);
        collectAdapter.setOnDataLoadFinish(this);
        recyclerView.addItemDecoration(new HorizDecoration(2));
        collectAdapter.loadDataFirstTime();
        recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
        recyclerView.setAdapter(collectAdapter);

        collectAdapter.setOnClickListener(new CollectAdapter.MyItemClickLinstener() {
            @Override
            public void OnCancleClickListener(CollectAdapter adapter, int position, View view) {
                adapter.notifyItemRemoved(position);
                int goodsId = Integer.valueOf(collectInfoList.get(position).getGoods_id());
                quXiaoShouChang(goodsId, position);
            }
        });


        collectAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object data) {
//                CollectBean.InfoBean.GoodinfoBean goodinfoBean = (CollectBean.InfoBean.GoodinfoBean) data;
//                startActivity(RebateGoodsDetailsActivity.createIntent(MyCollectActivity.this, goodinfoBean.getGoods_id(), goodinfoBean.getImg(), "1"));
            }
        });
    }

    private void quXiaoShouChang(int goodsId, final int position) {
        showDialog("加载中...");
        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        String token = sp.getString("token", "");
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("goodsId", goodsId);
        map.put("type", "2");   //取消收藏

        XUtil.Post(Url.GOODS_COLLECT, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);

                    if ("1".equals(jsonObject.optString("re"))) {
                        collectAdapter.removeItem(position);
                        initCollectList();
                    }

                    ToastUtils.showToast(MyCollectActivity.this, jsonObject.optString("info"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                closeDialog();
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });

    }


    @Override
    public void loadDataFinished(int datasLength) {
        if (datasLength == 0) {
            recyclerView.setVisibility(View.GONE);
            tv_collect.setVisibility(View.GONE);
            layEmtpy.setVisibility(View.VISIBLE);
            tv_msg.setText("暂无收藏商品~");
        } else {
            tv_collect.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.VISIBLE);
            layEmtpy.setVisibility(View.GONE);
        }
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }

    public void initCollectList(){
        showDialog("取消中...");
        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        String token = sp.getString("token", "");
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);

        XUtil.Post(Url.Ucenter_myCollecttion, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtils.e("==我的收藏==", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("re");
                    if (re.equals("1")){
                        initView();
                    }else if(re.equals("0")){
                        layEmtpy.setVisibility(View.VISIBLE);
                        recyclerView.setVisibility(View.GONE);
                        tv_collect.setVisibility(View.GONE);
                        tv_msg.setText("暂无收藏商品~");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinished() {
                super.onFinished();
              closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
             closeDialog();
            }
        });
    }
}
